using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;

namespace System.Web.Core
{
    public class MemberManager
    {
        private static MemberProvider memberProvider = Configuration.Instance.Providers["MemberProvider"] as MemberProvider;

        public static Member GetMember(string memberName)
        {
            return memberProvider.GetMember(memberName);
        }
        public static Member GetFirstMemberByMemberId(string memberId)
        {
            List<Member> members = GetMembersByMemberId(new Guid(memberId));
            if (members.Count > 0)
            {
                return members[0];
            }
            return null;
        }
        public static List<Member> GetMembers()
        {
            return memberProvider.GetMembers();
        }
        public static List<Member> GetMembersByMemberId(Guid memberId)
        {
            return memberProvider.GetMembersByMemberId(memberId);
        }
        public static void Create(Member member, MemberInfo memberInfo)
        {
            memberProvider.CreateMember(member);
            CreateSiteUsers(memberInfo);
        }
        public static void Delete(Guid memberId, string memberName)
        {
            memberProvider.DeleteMember(memberId, memberName);
            DeleteSiteUsers(memberId);
        }
        public static void UpdateMemberAttributes(Guid memberId, string attributeName, string attributeValue)
        {
            foreach (ISite site in Configuration.Instance.Sites.Values)
            {
                site.UpdateUserAttributes(memberId, attributeName, attributeValue);
            }
        }
        public static bool ValidateMember(Guid memberId, string password)
        {
            return memberProvider.ValidateMember(memberId, password);
        }
        public static bool ValidateMember(string memberName, string password)
        {
            return memberProvider.ValidateMember(memberName, password);
        }
        public static void ChangeMemberPassword(Member member, string newPassword)
        {
            memberProvider.ChangeMemberPassword(member, newPassword);
        }
        public static string ResetMemberPassword(Member member)
        {
            return memberProvider.ResetMemberPassword(member);
        }
        public static string GetMemberPassword(Member member)
        {
            return memberProvider.UnEncodePassword(member.Password, member.PasswordFormat);
        }

        public static void Login(string memberName)
        {
            Member member = MemberManager.GetMember(memberName);
            if (member != null)
            {
                CookieManager.SaveCookie(FormsAuthentication.GetAuthCookie(member.MemberId.ToString(), false));
                CookieManager.SaveCookie(new HttpCookie("MemberName", member.MemberName));
            }
        }

        public static void Login(string memberName, int expireDays)
        {
            Member member = MemberManager.GetMember(memberName);
            if (member != null)
            {
                CookieManager.SaveCookie(FormsAuthentication.GetAuthCookie(member.MemberId.ToString(), false), expireDays);
                CookieManager.SaveCookie(new HttpCookie("MemberName", member.MemberName), expireDays);
            }
        }

        public static void Logout()
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                FormsAuthentication.SignOut();
                CookieManager.DeleteCookie(FormsAuthentication.GetAuthCookie(HttpContext.Current.User.Identity.Name, false));
                CookieManager.DeleteCookie(HttpContext.Current.Request.Cookies["MemberName"]);
            }
        }

        private static void CreateSiteUsers(MemberInfo memberInfo)
        {
            if (memberInfo == null)
            {
                return;
            }
            foreach (ISite site in Configuration.Instance.Sites.Values)
            {
                site.CreateUser(memberInfo);
            }
        }
        private static void DeleteSiteUsers(Guid memberId)
        {
            foreach (ISite site in Configuration.Instance.Sites.Values)
            {
                site.DeleteUser(memberId);
            }
        }

        public static void RemoveAnonymousMember()
        {
            string cookieName = "AnonymousUserCookie";
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
            {
                CookieManager.DeleteCookie(cookie);
            }
        }
        public static Guid GetAnonymousMemberId()
        {
            Guid memberId = Guid.Empty;
            string cookieName = "AnonymousUserCookie";
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                memberId = Guid.NewGuid();
                cookie = new HttpCookie(cookieName);
            }
            else
            {
                try
                {
                    memberId = new Guid(cookie.Value);
                }
                catch
                {
                    memberId = Guid.NewGuid();
                }
            }

            cookie.Value = memberId.ToString();
            CookieManager.SaveCookie(cookie, 20);

            return memberId;
        }
    }
}